﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAtletIS.Models;

namespace MvcAtletIS.Controllers
{
    public class DataOutputController : Controller
    {
        atletisEntities db = new atletisEntities();
        
        // GET: /DataOutput/
        public ActionResult Index()
        {
            return View();
        }

        #region Competitions

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult Competitions(int count = 50)
        { 
            var model = db.Competitions
                .Where(c => c.Active == true & c.DateFrom < DateTime.Now)
                .OrderByDescending(c => c.DateFrom)
                .Take(count)
                .ToList();

            return Json(model.Select(m =>
                new
                {
                    m.ID,
                    m.Name,
                    m.DateFrom,
                    m.City,
                    m.Organiser,
                    m.External,
                    m.bResults,
                    m.sResults,
                    m.bPropositions,
                    m.sPropositions,
                }), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult CompetitionsByYearAndMonth(int year, int month)
        {
            var model = db.Competitions
                .Where(c => c.Active == true && c.DateFrom.Value.Year == year & c.DateFrom.Value.Month == month)
                .OrderByDescending(c => c.DateFrom)
                .ToList();

            return Json(model.Select(m =>
                new
                {
                    m.ID,
                    m.Name,
                    m.DateFrom,
                    m.City,
                    m.Organiser,
                    m.External,
                    m.bResults,
                    m.sResults,
                    m.bPropositions,
                    m.sPropositions,
                }), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult Calendar(int year, int month)
        {
            var model = db.Competitions
                .Where(c => c.Active == true && c.DateFrom.Value.Year == year & c.DateFrom.Value.Month == month)
                .OrderByDescending(c => c.DateFrom)
                .ToList();

            return Json(model.Select(m =>
                new CalendarEntity()
                {
                    id = m.ID,
                    title = m.Name,
                    url = m.sResults,
                    start = 1,
                    end = 2,
                }), JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region Articles

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult Articles(int count = 50, int max = 0)
        {
            var model = db.Articles
                .Where(a => a.Active == true & a.ID > max)
                .OrderByDescending(a => a.ID)
                .Take(count)
                .ToList();

            return Json(model.Select(m =>
                new
                {
                    m.ID,
                    m.CategoryId,
                    m.Title,
                    m.City,
                    m.Date,
                    m.Editor,
                    m.Perex,
                    m.Text,
                    m.ImagePath,
                    m.ImageText
                }), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult ArticlesLight(int count = 50, int max = 0)
        {
            var model = db.Articles
                .Where(a => a.Active == true & a.ID > max)
                .OrderByDescending(a => a.ID)
                .Take(count)
                .ToList();

            return Json(model.Select(m =>
                new
                {
                    m.ID,
                    m.CategoryId,
                    m.Title,
                    m.City,
                    m.Date,
                    m.Editor,
                    m.Perex,
                    m.ImagePath,
                    m.ImageText
                }), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult ArticleById(int id)
        {
            var model = db.Articles
                .FirstOrDefault(a => a.ID == id);

            return Json(new 
            { 
                model.ID, 
                model.CategoryId, 
                model.Title, 
                model.City, 
                model.Date, 
                model.Editor, 
                model.Perex, 
                model.Text, 
                model.ImagePath, 
                model.ImageText 
            }, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region Discipline list and performances

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult DisciplineList(int competitionId)
        {
            var model = db.DisciplineList
                .Where(dl => dl.CompetitionId == competitionId && dl.ParentId == 0 && dl.Active == true)
                .OrderBy(dl => dl.ExternalId)
                .ToList();

            return Json(model.Select(m =>
                new
                {
                    m.ID,
                    m.Name,
                    m.Description,
                    m.ExternalId,
                    m.ParentId,
                    m.DisciplineId,
                    m.CategoryId,
                    m.Day,
                    m.OfficialStartTime,
                    m.Status,
                    m.DisciplineCode
                }), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult Performances(int disciplineListId)
        {
            var model = db.Performances
                .Where(p => p.DisciplineListId == disciplineListId && p.Active == true)
                .OrderBy(p => p.Code)
                .ThenBy(p => p.Rank)
                .ThenBy(p => p.StartNumber)
                .ThenBy(p => p.CountryId)
                .ThenBy(p => p.Surname)
                .ThenBy(p => p.FirstName)
                .ToList();

            return Json(model.Select(m =>
                new
                {
                    m.ID,
                    m.DisciplineListId,
                    m.CompetitorId,
                    m.FirstName,
                    m.Surname,
                    m.Birthdate,
                    m.Club,
                    m.CountryId,
                    m.StartNumber,
                    m.Lane,
                    m.Code,
                    m.SB,
                    m.PB,
                    m.Result,
                    m.ResultQuality,
                    m.Rank,
                    m.Reaction,
                    m.Points
                }), JsonRequestBehavior.AllowGet);
        }

        #endregion



    }
}
